package com.buoy.base.aluminumpricespider.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buoy.base.aluminumpricespider.model.BaseMetalPrice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Mapper
public interface BaseMetalPriceMapper extends BaseMapper<BaseMetalPrice> {
    
    @Select("SELECT * FROM base_metal_price WHERE price_date = #{priceDate} AND price_source = #{priceSource} LIMIT 1")
    BaseMetalPrice findByPriceDateAndPriceSource(@Param("priceDate") LocalDate priceDate, @Param("priceSource") String priceSource);
    
    @Select("SELECT COUNT(*) > 0 FROM base_metal_price WHERE price_date = #{priceDate} AND price_source = #{priceSource}")
    boolean existsByPriceDateAndPriceSource(@Param("priceDate") LocalDate priceDate, @Param("priceSource") String priceSource);
    
    @Select("SELECT * FROM base_metal_price WHERE price_source = #{priceSource}")
    Page<BaseMetalPrice> findByPriceSource(Page<BaseMetalPrice> page, @Param("priceSource") String priceSource);
    
    @Select("SELECT * FROM base_metal_price WHERE price_date BETWEEN #{startDate} AND #{endDate}")
    Page<BaseMetalPrice> findByPriceDateBetween(Page<BaseMetalPrice> page, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
    
    @Select("SELECT * FROM base_metal_price WHERE price_source = #{priceSource} AND price_date BETWEEN #{startDate} AND #{endDate}")
    Page<BaseMetalPrice> findByPriceSourceAndPriceDateBetween(Page<BaseMetalPrice> page, @Param("priceSource") String priceSource, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
    
    @Select("SELECT * FROM base_metal_price WHERE price_source = #{priceSource} AND price_date <= #{priceDate} ORDER BY price_date DESC LIMIT #{days}")
    List<BaseMetalPrice> findPreviousDaysPrices(@Param("priceSource") String priceSource, @Param("priceDate") LocalDate priceDate, @Param("days") int days);
} 